<!DOCTYPE html>
<html>
<head>

	<meta http-equiv="Content-type" content="text/html; charset=utf-8">

	<title>Test Dijit Subscribe</title>

	<script type="text/javascript" src="../../dojo/dojo.js" data-dojo-config="isDebug: true"></script>
	<script type="text/javascript">
		dojo.require("doh.runner");
		dojo.require("dijit._Widget");
		dojo.require("dojo.parser");

		dojo.ready(function(){
			var numCallsInternal = 0;
			var numCallsExternal = 0;
			var externalString = "";
			var internalSubscribe = null;
	
			dojo.declare("MyWidget", dijit._Widget, {
				_subscribeHandlerInternal: function(){ numCallsInternal++; }
			});

			doh.register("parse", function(){
				dojo.parser.parse();
			});

			doh.register("_widgetSubscribe",
				[
					{
						name: "_Widget.subscribe - set up subscriptions",
						runTest: function(t){
							var w = dijit.byId("widget1");
							internalSubscribe = w.subscribe("/custom/event",
													"_subscribeHandlerInternal");
							w.subscribe("/custom/event", function(){
								if(this == w){
									numCallsExternal++;
								}
							});
							w.subscribe("/custom/setString", function(str){
								if(this == w){
									externalString = str;
								}
							});
							doh.is(0, numCallsInternal);
							doh.is(0, numCallsExternal);
							doh.is("", externalString);
							doh.isNot(null, internalSubscribe);
						}
					},
					{
						name: "_Widget.subscribe - publish events",
						runTest: function(t){
							var w = dijit.byId("widget1");
							dojo.publish("/custom/event", []);
							doh.is(1, numCallsInternal);
							doh.is(1, numCallsExternal);
							dojo.publish("/custom/setString", ["myString"]);
							doh.is("myString", externalString);
							dojo.publish("/custom/setString", ["anotherString"]);
							doh.is("anotherString", externalString);
						}
					},
					{
						name: "_Widget.unsubscribe",
						runTest: function(t){
							var w = dijit.byId("widget1");
							dojo.publish("/custom/event", []);
							doh.is(2, numCallsInternal, "numCallsInternal");
							doh.is(2, numCallsExternal, "numCallsExternal");
							w.unsubscribe(internalSubscribe);
							dojo.publish("/custom/event", []);
							doh.is(2, numCallsInternal, "numCallsInternal again");
							doh.is(3, numCallsExternal, "numCallsExternal again");
						}
					},
					{
						name: "_Widget.destroy",
						runTest: function(t){
							var w = dijit.byId("widget1");
							w.destroy();
							dojo.publish("/custom/event", []);
							doh.is(2, numCallsInternal, "numCallsInternal");
							doh.is(3, numCallsExternal, "numCallsExternal");
							dojo.publish("/custom/setString", ["myString"]);
							doh.is("anotherString", externalString, "externalString");
						}
					}
				]
			);

			doh.run();
		});

	</script>
</head>
<body class="claro">
	<div id="widget1" data-dojo-type="MyWidget"></div>
</body>
</html>
